package com.example.utils;

import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class NoteUtil {
    private final static int appid = 1400476303;
    private final static String appkey = "06281156cc8b3a9983d4fe18aaaa60b0";
    private final static int templateId = 847066;
    private final static String smsSign = "展望的个人博客";

    /**
     * 随机生成6位随机验证码
     * 方法说明
     *
     * @return String
     * @Discription:扩展说明
     */
    public static String createRandomVcode() {
        //验证码
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int) (Math.random() * 9);
        }
        return vcode;
    }

    /*
     *发送手机验证码(30分钟)
     */
    public static String sendSms(String phoneNumber) {
        try {
            String verifyCode = createRandomVcode();
            String[] params = {verifyCode};
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumber,
                    templateId, params, smsSign, "", "");
            System.out.println(result);
            if (result.result == 0) {
                return verifyCode;
            } else {
                return "结果没出来";
            }
        } catch (HTTPException e) {
            // HTTP 响应码错误
            e.printStackTrace();
            return "响应码错误";
        } catch (JSONException e) {
            // JSON 解析错误
            e.printStackTrace();
            return "解析错误";
        } catch (IOException e) {
            // 网络 IO 错误
            e.printStackTrace();
            return "网络IO错误";
        }
    }


}
